package com.carleasoft.mps.forum.socket;

import cn.hutool.json.JSONUtil;
import org.springframework.stereotype.Component;

import javax.websocket.OnClose;
import javax.websocket.OnMessage;
import javax.websocket.OnOpen;
import javax.websocket.Session;
import javax.websocket.server.PathParam;
import javax.websocket.server.ServerEndpoint;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

@Component
@ServerEndpoint("/webSocket/{userId}")
public class WebSocket {
    public WebSocket() {
        System.out.println("初始化  websocket");
    }

    private Session session;
    private static int onlineCount = 0;
    private static Map<String, WebSocket> clients = new ConcurrentHashMap<String, WebSocket>();
    private String userId;
    /**
     * 新建链接
     * @param userId
     * @param session
     */
    @OnOpen
    public void onOpen(@PathParam("userId") String userId, Session session){
        this.userId = userId;
        this.session=session;
        clients.put(this.userId, this);
        addOnlineCount();
        System.out.println("【websocket消息】有新的连接，总数：{}"+onlineCount);
    }

    /**
     * 断开链接
     */
    @OnClose
    public void onClose(){
        clients.remove(userId);
        subOnlineCount();
        System.out.println("【websocket消息】连接断开，总数：{}"+onlineCount);
    }

    /**
     * 接收消息
     * @param message
     */
    @OnMessage
    public void onMessage(String message){

//        for (WebSocket item : clients.values()) {
//
//            if (item.userId.equals(userId))
//
//                item.session.getAsyncRemote().sendText(message);
//
//        }
        System.out.println("【websocket消息】收到客户端发来的消息：{}"+message);
        this.sendMessageToUserId(message,"222");
    }

    /**
     * 发送到指定用户
     * @param message
     * @param ToUserId
     */
    public static void sendMessageToUserId(String message, String ToUserId){
        System.out.println(ToUserId + "#####"+message);
        for (WebSocket item : clients.values()) {
            if (item.userId.equals(ToUserId))
                item.session.getAsyncRemote().sendText(message);
        }

    }
    /**
     * 查询是否创建链接
     * @param ToUserId
     */
    public static int findToUserId(String ToUserId){
        int count = 0;

        for (WebSocket item : clients.values()) {

            if (item.userId.equals(ToUserId)){
                count++;
            }
        }
        return count;
    }
    /**
     * 消息发送给所有用户
     * @param message
     */
    public void sendMessageAll(String message){

        for (WebSocket item : clients.values()) {

            item.session.getAsyncRemote().sendText(message);

        }

    }

    public static synchronized int getOnlineCount() {

        return onlineCount;

    }

    public static synchronized void addOnlineCount() {

        WebSocket.onlineCount++;

    }

    public static synchronized void subOnlineCount() {

        WebSocket.onlineCount--;

    }

    public static synchronized Map<String, WebSocket> getClients() {

        return clients;

    }

}
